#!/usr/bin/python3

#    this file is part from AIA 
#    AIA is free software: you can redistribute it and/or modify
#    it under the terms of the GNU  General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    AIA is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License



import os
import gi
import xapp.os
import gettext
import signal
from gi.repository import Gtk
try:
    gi.require_version('AyatanaAppIndicator3', '0.1')
    from gi.repository import AyatanaAppIndicator3 as appindicator
except ValueError:
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import AppIndicator3 as appindicator

from gi.repository import GLib

gettext.install("AIA", "/usr/share/AIA/locale")
signal.signal(signal.SIGINT, signal.SIG_DFL)

ORCA_RUN_FLAG = os.path.expanduser("/etc/xdg/autostart/orca-autostart.desktop")

class AIA:
  def __init__(self):

    self.ind = appindicator.Indicator.new("new","access",appindicator.IndicatorCategory.APPLICATION_STATUS)
    self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)
    self.menu = Gtk.Menu()

    item = Gtk.MenuItem()
    item.set_label(_("Start Screen Reader"))
    item.connect("activate", self.start)
    self.menu.append(item)

    item = Gtk.MenuItem()
    item.set_label(_("Stop Screen Reader"))
    item.connect("activate", self.stop)
    self.menu.append(item)
    
    item = Gtk.MenuItem()
    item.set_label(_("Enable Autostart Screen Reader"))
    item.connect("activate", self.enable_autostart)
    self.menu.append(item)

    item = Gtk.MenuItem()
    item.set_label(_("Disable Autostart Screen Reader"))
    item.connect("activate", self.disable_autostart)
    self.menu.append(item)


    item = Gtk.MenuItem()
    item.set_label(_("Open Accessibility Settings"))
    item.connect("activate", self.settings)
    self.menu.append(item)


    item = Gtk.MenuItem()
    item.set_label(_("On Screen Keyboard"))
    item.connect("activate", self.On_Screen_Keyboard)
    self.menu.append(item)



    item = Gtk.MenuItem()
    item.set_label(_("Exit"))
    item.connect("activate", self.quit)
    self.menu.append(item)

    self.menu.show_all()
    self.ind.set_menu(self.menu)
    Gtk.main()



  def start(self, widget):
    os.system("nohup orca -r > /dev/null 2>&1&")

  def stop(self, widget):
    os.system("nohup killall orca > /dev/null 2>&1&")

  def enable_autostart(self, widget):
    if (xapp.os.is_live_session()) or (xapp.os.is_guest_session()):
        os.system("python3 /usr/lib/AIA/Live_Session_Message.py")
    elif os.path.exists(ORCA_RUN_FLAG):
        os.system("python3 /usr/lib/AIA/Already_Running_message.py")
    else :
        os.system('usudo "cp /usr/lib/AIA/orca-autostart.desktop /etc/xdg/autostart"')

  def disable_autostart(self, widget):
    if (xapp.os.is_live_session()) or (xapp.os.is_guest_session()):
        os.system("python3 /usr/lib/AIA/Live_Session_Message.py")
    elif not os.path.exists(ORCA_RUN_FLAG):
        os.system("python3 /usr/lib/AIA/Already_Stop_message.py")
    else :
        os.system('usudo "rm -rf /etc/xdg/autostart/orca-autostart.desktop"')


  def settings(self, widget):
    os.system("nohup orca -s > /dev/null 2>&1&")

  def On_Screen_Keyboard(self, widget):
    os.system("nohup onboard > /dev/null 2>&1&")

  def quit(self, widget):
    Gtk.main_quit()

indicator = AIA();

